package ro.ubb.web.author;

import java.io.IOException;
import java.io.Writer;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.ubb.dataaccess.entities.Author;
import ro.ubb.executor.BSException;
import ro.ubb.executor.Executor;

public class GetAuthorByName extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB(name = "ro.ubb.executor.Executor", beanName = "ExecutorEJB")
	private Executor executor;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		Writer out = response.getWriter();

		if (name == null || name.isEmpty()) {
			out.append("ERROR: name parameter null or empty!");
			return;
		}

		out.append("Result:\n");
		try {
			Author author = executor.getAuthorByName(name);
			out.append(author.toString());
		} catch (BSException be) {
			out.append(be.getMessage());
		}

	}
}
